package erp.server.role.controller;

import erp.server.base.response.JsonResult;
import erp.server.base.response.StatusCode;
import erp.server.role.mapper.RoleMapper;
import erp.server.role.pojo.dto.RoleQueryParam;
import erp.server.role.pojo.dto.RoleUpdateParam;
import erp.server.role.pojo.vo.RoleVO;
import erp.server.role.service.RoleService;
import erp.server.user.entity.dto.EditUserDTO;
import erp.server.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "角色模块")
@Slf4j
@RestController
@RequestMapping("/isms/role/")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private UserService userService;

    //查询加入组织的人员
    @ApiOperation("查询人员")
    @GetMapping("select")
    private JsonResult select(RoleQueryParam roleQueryParam){
        log.debug("roleQueryParam:{}",roleQueryParam);
        List<RoleVO> roleVOS = roleService.select(roleQueryParam);
        return JsonResult.ok(roleVOS);
    }

    //修改人员信息
    @ApiOperation("修改人员信息")
    @PostMapping("update")
    private JsonResult update(RoleUpdateParam roleUpdateParam){
        log.debug("roleUpdateParam:{}", roleUpdateParam);
        roleService.update(roleUpdateParam);

        EditUserDTO editUserDTO = new EditUserDTO();
        BeanUtils.copyProperties(roleUpdateParam,editUserDTO);
        editUserDTO.setId(roleUpdateParam.getUserId());
        log.debug("修改用户信息-入参:{}",editUserDTO);
        userService.editUserInfo(editUserDTO);
        return JsonResult.ok();
    }


    //删除人员信息
    @ApiOperation("删除人员信息")
    @PostMapping("delete/{userId}/{orgId}")
    private JsonResult delete(@PathVariable("userId") Integer userId,@PathVariable("orgId") Integer orgId){
        log.debug("userId:{}", userId);
        log.debug("orgId:{}",orgId);
        roleService.deleteUser(userId,orgId);
        return JsonResult.ok();
    }

    //修改状态
    @ApiOperation("修改状态")
    @PostMapping("updateStatus")
    private JsonResult updateStatus(RoleUpdateParam roleUpdateParam){
        log.debug("修改状态-入参:roleUpdateParam:{}",roleUpdateParam);
        roleService.updateStatus(roleUpdateParam);
        return JsonResult.ok();
    }

    //获取加入组织的人数
    @ApiOperation("获取加入组织人数")
    @GetMapping("count/{orgId}/{status}")
    public JsonResult countEmployees(@PathVariable("orgId") Integer orgId,@PathVariable("status") String status) {
        log.debug("获取加入组织的人数-入参:orgId:{},status:{}",orgId,status);
        int count = roleService.countEmployees(orgId,status);
        return JsonResult.ok(count);
    }
}
